home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume4 / xmodem < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  36.3 KB

  1. Subject: 4.2BSD XMODEM programs
  2. Newsgroups: mod.sources
  3. Approved: jpn@panda.UUCP
  4.  
  5. Mod.sources:  Volume 4, Issue 118
  6. Submitted by: seismo!uwvax!nicmad!brown (Mr. Video)
  7.  
  8. #! /bin/sh
  9. # This is a shell archive, meaning:
  10. # 1. Remove everything above the #! /bin/sh line.
  11. # 2. Save the resulting text in a file.
  12. # 3. Execute the file with /bin/sh (not csh) to create the files:
  13. #    README
  14. #    xmodrec.1
  15. #    xmodtran.1
  16. #    Makefile
  17. #    add.c
  18. #    addcr.c
  19. #    addlf.c
  20. #    copy.c
  21. #    cr.c
  22. #    cr2lf.c
  23. #    del.c
  24. #    delcr.c
  25. #    lf.c
  26. #    lf2cr.c
  27. #    receive.c
  28. #    to_pc.c
  29. #    to_vax.c
  30. #    transmit.c
  31. # This archive created: Mon May 12 23:22:53 1986
  32. export PATH; PATH=/bin:$PATH
  33. echo shar: extracting "'README'" '(684 characters)'
  34. if test -f 'README'
  35. then
  36.     echo shar: will not over-write existing file "'README'"
  37. else
  38. cat << \SHAR_EOF > 'README'
  39. To create the two xmodem programs, run:
  40.     make all
  41.  
  42. To (compile and) install run:
  43.     make install
  44.  
  45. There are two manual files: xmodtran.1 and xmodrec.1
  46.  
  47. The two compiled programs, xmodrec and xmodtran, will be moved to the
  48. /usr/local directory.  If this is not where you want them, change the
  49. Makefile $PATH variable to what you want for your system.
  50.  
  51. The programs are being used on a VAX 750 with BSD 4.2.  They are used
  52. to transfer data between PCs and the VAX.
  53.  
  54. If you have any questions or comments, send UUCP mail to:
  55.  
  56.               ihnp4------\
  57.             harvard-\     \
  58.                seismo!uwvax!nicmad!brown
  59.               topaz-/     /
  60.              decvax------/
  61.  
  62. Mike Brown
  63. SHAR_EOF
  64. if test 684 -ne "`wc -c < 'README'`"
  65. then
  66.     echo shar: error transmitting "'README'" '(should have been 684 characters)'
  67. fi
  68. fi
  69. echo shar: extracting "'xmodrec.1'" '(579 characters)'
  70. if test -f 'xmodrec.1'
  71. then
  72.     echo shar: will not over-write existing file "'xmodrec.1'"
  73. else
  74. cat << \SHAR_EOF > 'xmodrec.1'
  75. .TH XMODREC 1
  76. .SH NAME
  77. xmodrec \- receive file using XMODEM protocol
  78. .SH SYNOPSIS
  79. .B xmodrec
  80. [
  81. .B \-t
  82. ] filename
  83. .SH DESCRIPTION
  84. .I Xmodrec
  85. is used to receive a file from another computer, ie, IBM PC or look-alike.
  86. The computer from which the file is to be received must also have the
  87. XMODEM protocol.
  88. .PP
  89. This version of XMODEM uses the checksum method of error detection.
  90. .PP
  91. Options:
  92. .TP 6
  93. .B  \-t
  94. Remove the carriage returns from the incoming data.  Also removes the ^Z
  95. end-of-file marker and any data after it.
  96. .SH AUTHOR
  97. Jim Hein {ihnp4,seismo}!uwvax!astroatc!nicmad!jhein
  98. SHAR_EOF
  99. if test 579 -ne "`wc -c < 'xmodrec.1'`"
  100. then
  101.     echo shar: error transmitting "'xmodrec.1'" '(should have been 579 characters)'
  102. fi
  103. fi
  104. echo shar: extracting "'xmodtran.1'" '(614 characters)'
  105. if test -f 'xmodtran.1'
  106. then
  107.     echo shar: will not over-write existing file "'xmodtran.1'"
  108. else
  109. cat << \SHAR_EOF > 'xmodtran.1'
  110. .TH XMODTRAN 1
  111. .SH NAME
  112. xmodtran \- transmit a file using XMODEM protocol
  113. .SH SYNOPSIS
  114. .B xmodtran
  115. [
  116. .B \-t
  117. ] filename
  118. .SH DESCRIPTION
  119. .I Xmodtran
  120. is used to transmit a file to another computer, ie, IBM PC or look-alike.
  121. The computer to which the file is to be transmitted must also have the
  122. XMODEM protocol.
  123. .PP
  124. This version of XMODEM uses the checksum method of error detection.
  125. .PP
  126. Options:
  127. .TP 6
  128. .B  \-t
  129. Adds carriage returns to each line of data being transmitted.  Some computers
  130. require that the text file have a CR/LF at the end of each line.
  131. .SH AUTHOR
  132. Jim Hein {ihnp4,seismo}!uwvax!astroatc!nicmad!jhein
  133. SHAR_EOF
  134. if test 614 -ne "`wc -c < 'xmodtran.1'`"
  135. then
  136.     echo shar: error transmitting "'xmodtran.1'" '(should have been 614 characters)'
  137. fi
  138. fi
  139. echo shar: extracting "'Makefile'" '(1340 characters)'
  140. if test -f 'Makefile'
  141. then
  142.     echo shar: will not over-write existing file "'Makefile'"
  143. else
  144. cat << \SHAR_EOF > 'Makefile'
  145. FILES    = receive.c transmit.c addcr.c delcr.c add.c del.c\
  146.           lf2cr.c cr.c lf.c cr2lf.c
  147. OBJS    = receive.o transmit.o addcr.o delcr.o add.o del.o\
  148.           lf2cr.o cr.o lf.o cr2lf.o
  149. LIBS     = -lcurses -ltermlib
  150. FLAGS    = -O -c -I/sys/h
  151. PATH    = /usr/local
  152. all:        receive transmit
  153. clean:
  154.         rm -f $(OBJS) receive transmit
  155. pr:
  156.         lpr -Plp $(FILES)
  157. install:    receive transmit
  158.         strip receive
  159.         cp receive $(PATH)/xmodrec
  160.         strip transmit
  161.         cp transmit $(PATH)/xmodtran
  162. receive.o:    receive.c
  163.         cc $(FLAGS) receive.c
  164. receive:    receive.o del.o lf.o
  165.         cc -O -o receive del.o lf.o receive.o $(LIBS)
  166. transmit.o:    transmit.c
  167.         cc $(FLAGS) transmit.c
  168. transmit:    transmit.o add.o cr.o
  169.         cc -O -o transmit add.o cr.o transmit.o $(LIBS)
  170. addcr.o:    addcr.c
  171.         cc $(FLAGS) addcr.c
  172. addcr:        addcr.o add.o
  173.         cc -O -o addcr add.o addcr.o
  174.         strip addcr
  175.         cp addcr $(PATH)
  176. delcr.o:    delcr.c
  177.         cc $(FLAGS) delcr.c
  178. delcr:        delcr.o del.o
  179.         cc -O -o delcr del.o delcr.o
  180.         strip delcr
  181.         cp delcr $(PATH)
  182. add.o:        add.c
  183.         cc $(FLAGS) add.c
  184. del.o:        del.c
  185.         cc $(FLAGS) del.c
  186. cr.o:        cr.c
  187.         cc $(FLAGS) cr.c
  188. lf2cr.o:    lf2cr.c
  189.         cc $(FLAGS) lf2cr.c
  190. lf2cr:        lf2cr.o cr.o
  191.         cc -O -o lf2cr lf2cr.o cr.o
  192.         strip lf2cr
  193.         cp lf2cr $(PATH)
  194. lf.o:        lf.c
  195.         cc $(FLAGS) lf.c
  196. cr2lf.o:    cr2lf.c
  197.         cc $(FLAGS) cr2lf.c
  198. cr2lf:        cr2lf.o lf.o
  199.         cc -O -o cr2lf cr2lf.o lf.o
  200.         strip cr2lf
  201.         cp cr2lf $(PATH)
  202. SHAR_EOF
  203. if test 1340 -ne "`wc -c < 'Makefile'`"
  204. then
  205.     echo shar: error transmitting "'Makefile'" '(should have been 1340 characters)'
  206. fi
  207. fi
  208. echo shar: extracting "'add.c'" '(881 characters)'
  209. if test -f 'add.c'
  210. then
  211.     echo shar: will not over-write existing file "'add.c'"
  212. else
  213. cat << \SHAR_EOF > 'add.c'
  214. #
  215. # include <file.h>
  216.  
  217. # define CR    13
  218. # define NL    10
  219. # define CTRL_Z 26
  220.  
  221. char *
  222. ADDCR(file)
  223. char *file; {
  224.     int id, oid;
  225.     char buf[512], *index();
  226.     static char temp[11];
  227.     char *end = "\r\n";
  228.     int ln, cnt;
  229.     register char *ptr = buf, *od;
  230.     register i, count;
  231.  
  232.     strcpy(temp,"tempXXXXXX");
  233.     mktemp(temp);
  234.     if ((oid = open(temp,O_CREAT | O_WRONLY,0644)) < 0) {
  235.         perror(temp);
  236.         unlink(temp);
  237.         exit(-1);
  238.     }
  239.     if ((id = open(file,O_RDONLY,0644)) < 0) {
  240.         perror(file);
  241.         unlink(temp);
  242.         exit(-1);
  243.     }
  244.             
  245.     while (ln = read(id,ptr,512)) {
  246.         count = 0;
  247.         while ( od = index(ptr,NL) ) {
  248.             if (od > &buf[ln]) break;
  249.             *od = '\0';
  250.             cnt = strlen(ptr);
  251.             count += cnt;
  252.             write(oid, ptr, cnt);
  253.             write(oid, end, 2);
  254.             ptr = ++od;
  255.             count++;
  256.         }
  257.         if (count = ln - count)
  258.             write(oid,ptr,count);
  259.         ptr = buf;
  260.     }
  261.     *ptr = CTRL_Z;
  262.     write(oid,ptr,1);
  263.  
  264.     close(oid); close(id);
  265.     return(temp);
  266. }
  267. SHAR_EOF
  268. if test 881 -ne "`wc -c < 'add.c'`"
  269. then
  270.     echo shar: error transmitting "'add.c'" '(should have been 881 characters)'
  271. fi
  272. fi
  273. echo shar: extracting "'addcr.c'" '(219 characters)'
  274. if test -f 'addcr.c'
  275. then
  276.     echo shar: will not over-write existing file "'addcr.c'"
  277. else
  278. cat << \SHAR_EOF > 'addcr.c'
  279. main(argc,argv)
  280. int argc;
  281. char *argv[]; {
  282.     char buf[512];
  283.     register int i;
  284.  
  285.     for (i = 1; i < argc; i++) {
  286.         strcpy(buf,"mv ");
  287.         strcat(buf,ADDCR(argv[i]));
  288.         strcat(buf," ");
  289.         strcat(buf,argv[i]);
  290.         system(buf);
  291.     }
  292. }
  293. SHAR_EOF
  294. if test 219 -ne "`wc -c < 'addcr.c'`"
  295. then
  296.     echo shar: error transmitting "'addcr.c'" '(should have been 219 characters)'
  297. fi
  298. fi
  299. echo shar: extracting "'addlf.c'" '(935 characters)'
  300. if test -f 'addlf.c'
  301. then
  302.     echo shar: will not over-write existing file "'addlf.c'"
  303. else
  304. cat << \SHAR_EOF > 'addlf.c'
  305. #
  306. # define CR    13
  307. # define NL    10
  308.  
  309. main(argc,argv)
  310. int argc;
  311. char *argv[]; {
  312.     int id, oid;
  313.     char buf[512];
  314.     char *temp, *index();
  315.     char *end = "\r\n";
  316.     int ln, cnt;
  317.     register char *ptr, *od;
  318.     register i, count;
  319.  
  320.     for (i = 1; i < argc; i++) {
  321.         if ((id = open(argv[i],0)) < 0) {
  322.             printf("CAN NOT OPEN FILE `%s'. . . . .\n",argv[i]);
  323.             break;
  324.         } else {
  325.             temp = "tempXXXXXX";
  326.             mktemp(temp);
  327.             oid = creat(temp,0644);
  328.             
  329.             ptr = buf;
  330.             while (ln = read(id,ptr,512)) {
  331.                 count = 0;
  332.                 while ( od = index(ptr,CR) ) {
  333.                     if (od > &buf[ln]) break;
  334.                     *od = '\0';
  335.                     cnt = strlen(ptr);
  336.                     count += cnt;
  337.                     write(oid, ptr, cnt);
  338.                     write(oid, end, 2);
  339.                     ptr = ++od;
  340.                     count++;
  341.                 }
  342.                 if (count = ln - count)
  343.                     write(oid,ptr,count);
  344.                 ptr = buf;
  345.             }
  346.  
  347.             close(oid); close(id);
  348.             strcpy(buf,"mv ");
  349.             strcat(buf,temp);
  350.             strcat(buf," ");
  351.             strcat(buf,argv[i]);
  352.             system(buf);
  353.             unlink(temp);
  354.         }
  355.     }
  356. }
  357. SHAR_EOF
  358. if test 935 -ne "`wc -c < 'addlf.c'`"
  359. then
  360.     echo shar: error transmitting "'addlf.c'" '(should have been 935 characters)'
  361. fi
  362. fi
  363. echo shar: extracting "'copy.c'" '(6082 characters)'
  364. if test -f 'copy.c'
  365. then
  366.     echo shar: will not over-write existing file "'copy.c'"
  367. else
  368. cat << \SHAR_EOF > 'copy.c'
  369. #
  370. #include <stdio.h>
  371. #include <sys/types.h>
  372. #include <sys/dir.h>
  373. #include <sys/stat.h>
  374. #include <signal.h>
  375. #include <pwd.h>
  376. #include <grp.h>
  377.  
  378.  
  379. /*
  380. **    copy - copy a  subtree to a new directory
  381. **    preserving ownership and dates (access and modification only)
  382. **
  383. **    Keith Thompson    - Feb 7, 1981
  384. **    revised        - May 1, 1981    (added options)
  385. **
  386. **    Options:    (can only be used by superuser)
  387. **
  388. **        -o login_name    make all files owned by 'login_name'
  389. **        -u login_name    make all user ownership 'login_name'
  390. **        -g group_name    make all group ownership 'group_name'
  391. */
  392.  
  393. #define BSIZE 4096
  394.  
  395. #define DEST (argc-1)
  396. #define SRC  (argc-2)
  397.  
  398. #define EXISTS 0
  399. #define READABLE 4
  400. #define WRITEABLE 2
  401. #define EXECUTABLE 1
  402.  
  403. #define MODE 0666
  404. #define READ 0
  405.  
  406. #define strncat strcatn
  407. extern char *strcat();
  408. extern char *strcpy();
  409.  
  410. int superuser;
  411. int user;
  412. int group;
  413.  
  414. main(argc,argv)
  415. int argc;
  416. char **argv;
  417. {
  418.     register i,j,badarg;
  419.     register struct passwd *pw;
  420.     extern struct passwd *getpwnam();
  421.  
  422.     signal(SIGINT,SIG_IGN);
  423.     signal(SIGHUP,SIG_IGN);
  424.     signal(SIGQUIT,SIG_IGN);
  425.     signal(SIGTERM,SIG_IGN);
  426.     signal(SIGPIPE,SIG_IGN);
  427.  
  428.     user = getuid();
  429.     superuser = user == 0;
  430.     group = superuser ? 0 : getgid();
  431.  
  432.     badarg = 0;
  433.  
  434.     if(argc < 3) {
  435.         fprintf(stderr,"usage: %s [ options ] src_dir dst_dir\n",
  436.                 argv[0]);
  437.         exit(2);
  438.     }
  439.  
  440.     if(superuser) for(i=1 ; i<SRC; i = j) {
  441.         register char *ptr;
  442.  
  443.         ptr = argv[i];
  444.         j = i+1;
  445.         if(j >= SRC) {
  446.             fprintf(stderr,"Invalid argument: %s\n",argv[i]);
  447.             badarg++;
  448.             break;
  449.         }
  450.  
  451.         if(*ptr++ != '-') {
  452.             fprintf(stderr,"Invalid argument: %s\n",argv[i]);
  453.             badarg++;
  454.             continue;
  455.         }
  456.  
  457.         pw = getpwnam(argv[j]);
  458.         j++;
  459.  
  460.         switch(*ptr) {
  461.  
  462.         case 'o':
  463.             user = pw->pw_uid;
  464.             group = pw->pw_gid;
  465.             break;
  466.  
  467.         case 'u':
  468.             user = pw->pw_uid;
  469.             break;
  470.  
  471.         case 'g':
  472.             group = pw->pw_gid;
  473.             break;
  474.  
  475.         default:
  476.             fprintf(stderr,"Bad option: %c\n",*(ptr-1));
  477.             badarg++;
  478.         }
  479.     }
  480.  
  481.     if(badarg) exit(4);
  482.  
  483.  
  484.     if(!access(argv[DEST],EXISTS)) {
  485.         fprintf(stderr,"%s already exists\n",argv[DEST]);
  486.         exit(2);
  487.     }
  488.  
  489.     if(access(argv[SRC],READABLE)) {
  490.         fprintf(stderr,"%s not readable\n",argv[SRC]);
  491.         exit(2);
  492.     }
  493.  
  494.  
  495.     copy(argv[SRC],argv[DEST]);
  496. }
  497.  
  498. copy(src,dst)
  499. char *src,*dst;
  500. {
  501.     struct direct dirbuf;
  502.     union {
  503.         char *c;
  504.         struct direct *d;
  505.     } u_dir;
  506.     struct stat statbuf;
  507.     struct stat srcstat;
  508.     char src_file[256],dst_file[256];
  509.     int src_fd;
  510.  
  511.     u_dir.d = &dirbuf;
  512.  
  513.     if(stat(src,&srcstat)) {
  514.         perror(src);
  515.         exit(4);
  516.     }
  517.  
  518.     if((srcstat.st_mode&S_IFMT) != S_IFDIR) {
  519.         fprintf(stderr,"copy: %s is not a directory\n",src);
  520.         exit(4);
  521.     }
  522.  
  523.     /*
  524.     **    src exists and is readable
  525.     **    now make the target directory
  526.     */
  527.  
  528.     if(mkdir(dst)) {
  529.         exit(4);
  530.     }
  531.  
  532.     change(dst, &srcstat);
  533.  
  534.     if((src_fd = open(src,0)) < 0) {
  535.         perror(src);
  536.         exit(4);
  537.     }
  538.  
  539.     while(read(src_fd,u_dir.c,sizeof(dirbuf)) == sizeof(dirbuf)) {
  540.  
  541.         /*
  542.         **    test to see if the directory slot is in use
  543.         */
  544.  
  545.         if(!dirbuf.d_ino)  {
  546.             continue;
  547.         }
  548.  
  549.         /*
  550.         **    skip "." & "..", mkdir takes care of them
  551.         */
  552.  
  553.         if(!strcmp(".",dirbuf.d_name)
  554.         || !strcmp("..",dirbuf.d_name)) {
  555.             continue;
  556.         }
  557.  
  558.         /*
  559.         **    we have a file or directory to copy now
  560.         */
  561.  
  562.         mkname(src_file,src,dirbuf.d_name);
  563.         mkname(dst_file,dst,dirbuf.d_name);
  564.  
  565.         if(stat(src_file,&statbuf)) {
  566.             perror(src_file);
  567.             exit(4);
  568.         }
  569.  
  570.         if(!superuser && access(src_file,READABLE)) {
  571.             fprintf(stdout,"copy: no access %s\n",src_file);
  572.             continue;
  573.         }
  574.  
  575.         /*
  576.         **    do the copy depending on the file type
  577.         **
  578.         **    directory:    recursively copy the subtree
  579.         **    regular:    copy the file
  580.         **    special:    make a new node (super user only)
  581.         */
  582.  
  583.         switch((statbuf.st_mode)&S_IFMT) {
  584.  
  585.         case S_IFDIR:
  586.             copy(src_file,dst_file);
  587.             break;
  588.  
  589.         case S_IFCHR:
  590.         case S_IFBLK:
  591.             if(!superuser) break;
  592.  
  593.             fprintf(stdout,"making special file %s\n",dst_file);
  594.  
  595.             if(mknod(dst_file,(int) statbuf.st_mode,
  596.                 statbuf.st_rdev)) {
  597.             }
  598.             change(dst_file, &statbuf);
  599.             utime(dst_file,&(statbuf.st_atime));
  600.             if(chmod(dst_file,(int) statbuf.st_mode)) {
  601.                 perror(dst_file);
  602.                 exit(4);
  603.             }
  604.             break;
  605.  
  606.         case S_IFREG:
  607.             filecopy(src_file,dst_file);
  608.             change(dst_file, &statbuf);
  609.             utime(dst_file,&(statbuf.st_atime));
  610.             if(chmod(dst_file,(int) statbuf.st_mode)) {
  611.                 perror(dst_file);
  612.                 exit(4);
  613.             }
  614.             break;
  615.  
  616.         default:
  617.             fprintf(stderr,"bad file type: %s\n",src_file);
  618.         }
  619.  
  620.     }
  621.     utime(dst,&(srcstat.st_atime));
  622.     if(chmod(dst,(int) srcstat.st_mode)) {
  623.         perror(dst_file);
  624.         exit(4);
  625.     }
  626.  
  627.     close(src_fd);
  628. }
  629.  
  630.  
  631. mkname(dst,str1,str2)
  632. char *dst,*str1,*str2;
  633. {
  634.     dst[0] = '\0';
  635.     strcat(dst,str1);
  636.     strcat(dst,"/");
  637.     strncat(dst,str2,14);
  638.  
  639. }
  640.  
  641. filecopy(src,dst)
  642. char *src,*dst;
  643. {
  644.     register int src_fd,dst_fd;
  645.     char block[BSIZE];
  646.     register int size;
  647.  
  648.     if((src_fd = open(src,READ)) < 0) {
  649.         perror(src);
  650.         exit(4);
  651.     }
  652.  
  653.     if((dst_fd = creat(dst,MODE)) < 0) {
  654.         perror(dst);
  655.         exit(4);
  656.     }
  657.  
  658.     while((size = read(src_fd,block,BSIZE)) > 0) {
  659.         if(write(dst_fd,block,size) != size) {
  660.             perror(dst);
  661.             exit(4);
  662.         }
  663.     }
  664.  
  665.     if(size < 0) {
  666.         perror(src);
  667.         exit(4);
  668.     }
  669.  
  670.     if(close(src_fd)) {
  671.         perror(src);
  672.         exit(4);
  673.     }
  674.     if(close(dst_fd)) {
  675.         perror(src);
  676.         exit(4);
  677.     }
  678.  
  679. }
  680.  
  681. change(file,status)
  682. char *file;
  683. struct stat *status;
  684. {
  685.     int u,g;
  686.  
  687.     u = user;
  688.     g = group;
  689.  
  690.     if(superuser) {
  691.         if(!user) {
  692.             u = status->st_uid;
  693.         }
  694.         if(!group) {
  695.             g = status->st_gid;
  696.         }
  697.     }
  698.     if(chown(file, u, g)) {
  699.         perror(file);
  700.         exit(4);
  701.     }
  702. }
  703.  
  704.  
  705. mkdir(d)
  706. register char *d;
  707. {
  708.     char pname[128], dname[128];
  709.     register i, slash = 0;
  710.  
  711.     pname[0] = '\0';
  712.     for(i = 0; d[i]; ++i)
  713.         if(d[i] == '/')
  714.             slash = i + 1;
  715.     if(slash)
  716.         strncpy(pname, d, slash);
  717.     strcpy(pname+slash, ".");
  718.     if (access(pname, WRITEABLE)) {
  719.         fprintf(stderr,"mkdir: cannot access %s\n", pname);
  720.         return(-1);
  721.     }
  722.  
  723.     if ((mknod(d, 040777, 0)) < 0) {
  724.         fprintf(stderr,"mkdir: cannot make directory %s\n", d);
  725.         return(-1);
  726.     }
  727.     strcpy(dname, d);
  728.     strcat(dname, "/.");
  729.  
  730.     if((link(d, dname)) < 0) {
  731.         fprintf(stderr, "mkdir: cannot link %s\n", dname);
  732.         unlink(d);
  733.         return(-1);
  734.     }
  735.     strcat(dname, ".");
  736.     if((link(pname, dname)) < 0) {
  737.         fprintf(stderr, "mkdir: cannot link %s\n",dname);
  738.         dname[strlen(dname)] = '\0';
  739.         unlink(dname);
  740.         unlink(d);
  741.         return(-1);
  742.     }
  743.  
  744.     return(0);
  745. }
  746. SHAR_EOF
  747. if test 6082 -ne "`wc -c < 'copy.c'`"
  748. then
  749.     echo shar: error transmitting "'copy.c'" '(should have been 6082 characters)'
  750. fi
  751. fi
  752. echo shar: extracting "'cr.c'" '(778 characters)'
  753. if test -f 'cr.c'
  754. then
  755.     echo shar: will not over-write existing file "'cr.c'"
  756. else
  757. cat << \SHAR_EOF > 'cr.c'
  758. #
  759. #include <file.h>
  760.  
  761. #define CR    13
  762. #define NL    10
  763.  
  764. char *
  765. LF2CR(file)
  766. char *file; {
  767.     int id, oid;
  768.     char buf[512], *index();
  769.     static char temp[11];
  770.     register char *ptr = buf;
  771.     register ln, count;
  772.  
  773.     strcpy(temp,"tempXXXXXX");
  774.     mktemp(temp);
  775.     if ((oid = open(temp,O_CREAT | O_WRONLY,0644)) < 0) {
  776.         perror(temp);
  777.         unlink(temp);
  778.         exit(-1);
  779.     }
  780.     if ((id = open(file,O_RDONLY,0644)) < 0) {
  781.         perror(file);
  782.         unlink(temp);
  783.         exit(-1);
  784.     }
  785.             
  786.     count = 0;
  787.     while (ln = read(id,ptr,512)) {
  788.         while ( ptr = index(ptr,NL) ) {
  789.             if (ptr > &buf[ln]) break;
  790.             *ptr = CR;
  791.         }
  792.         write(oid,ptr = buf,ln);
  793.         count += ln;
  794.     }
  795.     if (ln = count % 128) {
  796.         count = 128 - ln;
  797.         for (ln = 0, ptr = buf; ln < count ; ln++, *ptr++ = ' ');
  798.         write(oid,buf,ln);
  799.     }
  800.  
  801.     close(oid); close(id);
  802.     return(temp);
  803. }
  804. SHAR_EOF
  805. if test 778 -ne "`wc -c < 'cr.c'`"
  806. then
  807.     echo shar: error transmitting "'cr.c'" '(should have been 778 characters)'
  808. fi
  809. fi
  810. echo shar: extracting "'cr2lf.c'" '(129 characters)'
  811. if test -f 'cr2lf.c'
  812. then
  813.     echo shar: will not over-write existing file "'cr2lf.c'"
  814. else
  815. cat << \SHAR_EOF > 'cr2lf.c'
  816. main(argc,argv)
  817. int argc;
  818. char *argv[]; {
  819.     char buf[512];
  820.     register int i;
  821.  
  822.     for (i = 1; i < argc; i++) {
  823.         CR2LF(argv[i]);
  824.     }
  825. }
  826. SHAR_EOF
  827. if test 129 -ne "`wc -c < 'cr2lf.c'`"
  828. then
  829.     echo shar: error transmitting "'cr2lf.c'" '(should have been 129 characters)'
  830. fi
  831. fi
  832. echo shar: extracting "'del.c'" '(1030 characters)'
  833. if test -f 'del.c'
  834. then
  835.     echo shar: will not over-write existing file "'del.c'"
  836. else
  837. cat << \SHAR_EOF > 'del.c'
  838. #
  839. # include <file.h>
  840.  
  841. # define CR    13
  842. # define NL    10
  843. # define CTRL_Z    26
  844. # define BLOCK    512
  845.  
  846. DELCR(file)
  847. char *file; {
  848.     int id, oid;
  849.     char buf[BLOCK+10], *index();
  850.     static char temp[11];
  851.     register char *ptr = buf, *od, *cz;
  852.     register int len;
  853.  
  854.     strcpy(temp,"tempXXXXXX");
  855.     mktemp(temp);
  856.     if ((oid = open(temp,O_CREAT | O_WRONLY,0644)) < 0) {
  857.         perror(temp);
  858.         unlink(temp);
  859.         exit(-1);
  860.     }
  861.  
  862.     if ((id = open(file,O_RDONLY,0644)) < 0) {
  863.         perror(file);
  864.         unlink(temp);
  865.         exit(-1);
  866.     }
  867.  
  868.     while ((len = read(id,ptr,BLOCK)) > 0) {
  869.         *(ptr + len) = 0;
  870.         while ( od = index(ptr,CR) ) {
  871.             *od = '\0';
  872.             if (cz = index(ptr,CTRL_Z)) {
  873.                 *cz = '\0';
  874.                 write(oid, ptr, strlen(ptr));
  875.                  goto OUT;
  876.             }
  877.             write(oid, ptr, strlen(ptr));
  878.             ptr = ++od;
  879.         }
  880.         if (cz = index(ptr,CTRL_Z)) {
  881.             *cz = '\0';
  882.             write(oid, ptr, strlen(ptr));
  883.              goto OUT;
  884.         }
  885.         if (len = strlen(ptr))
  886.         write(oid, ptr, len);
  887.         ptr = buf;
  888.     }
  889.  
  890. OUT:
  891.     close(oid); close(id);
  892.     strcpy(buf,"mv ");
  893.     strcat(buf,temp);
  894.     strcat(buf," ");
  895.     strcat(buf,file);
  896.     system(buf);
  897. }
  898. SHAR_EOF
  899. if test 1030 -ne "`wc -c < 'del.c'`"
  900. then
  901.     echo shar: error transmitting "'del.c'" '(should have been 1030 characters)'
  902. fi
  903. fi
  904. echo shar: extracting "'delcr.c'" '(129 characters)'
  905. if test -f 'delcr.c'
  906. then
  907.     echo shar: will not over-write existing file "'delcr.c'"
  908. else
  909. cat << \SHAR_EOF > 'delcr.c'
  910. main(argc,argv)
  911. int argc;
  912. char *argv[]; {
  913.     char buf[512];
  914.     register int i;
  915.  
  916.     for (i = 1; i < argc; i++) {
  917.         DELCR(argv[i]);
  918.     }
  919. }
  920. SHAR_EOF
  921. if test 129 -ne "`wc -c < 'delcr.c'`"
  922. then
  923.     echo shar: error transmitting "'delcr.c'" '(should have been 129 characters)'
  924. fi
  925. fi
  926. echo shar: extracting "'lf.c'" '(1252 characters)'
  927. if test -f 'lf.c'
  928. then
  929.     echo shar: will not over-write existing file "'lf.c'"
  930. else
  931. cat << \SHAR_EOF > 'lf.c'
  932. #
  933. #include <file.h>
  934.  
  935. #define CR    13
  936. #define NL    10
  937. #define BLOCK    512
  938. #define STOP    0xff
  939.  
  940. CR2LF(file)
  941. char *file; {
  942.     int id, oid;
  943.     char buf[BLOCK+1], *index();
  944.     char temp[11], temp1[11];
  945.     register char *ptr = buf;
  946.     register int len;
  947.  
  948.     strcpy(temp,"tempXXXXXX");
  949.     mktemp(temp);
  950.     if ((oid = open(temp,O_CREAT | O_RDWR,0644)) < 0) {
  951.         perror(temp);
  952.         unlink(temp);
  953.         exit(-1);
  954.     }
  955.  
  956.     if ((id = open(file,O_RDONLY,0644)) < 0) {
  957.         perror(file);
  958.         unlink(temp);
  959.         exit(-1);
  960.     }
  961.  
  962.     buf[BLOCK] = 0;
  963.     while ((len = read(id,ptr,BLOCK)) > 0) {
  964.         while ( ptr = index(ptr,CR) ) {
  965.             *ptr = NL;
  966.         }
  967.         write(oid, ptr = buf, len);
  968.     }
  969.     
  970.     close(id);
  971.     lseek(oid,1,L_XTND);
  972.     do {
  973.         lseek(oid,-2,L_INCR);
  974.         read(oid,ptr,1);
  975.     } while ( *ptr != NL );
  976.     *ptr = STOP;
  977.     write(oid,ptr,1);
  978.     lseek(oid,0,L_SET);
  979.  
  980.     strcpy(temp1,"tempXXXXXX");
  981.     mktemp(temp1);
  982.     if ((id = open(temp1,O_CREAT | O_WRONLY,0644)) < 0) {
  983.         perror(temp1);
  984.         unlink(temp1);
  985.         unlink(temp);
  986.         exit(-1);
  987.     }
  988.  
  989.     ptr = buf;
  990.     while ((len = read(oid,ptr,BLOCK)) > 0) {
  991.         if (ptr = index(ptr,STOP))  {
  992.             *ptr = 0;
  993.             write(id, buf, strlen(buf));
  994.             break;
  995.         }
  996.         write(id, ptr = buf, len);
  997.     }
  998.  
  999.     close(oid); close(id);
  1000.     strcpy(buf,"mv ");
  1001.     strcat(buf,temp1);
  1002.     strcat(buf," ");
  1003.     strcat(buf,file);
  1004.     system(buf);
  1005.     unlink(temp);
  1006. }
  1007. SHAR_EOF
  1008. if test 1252 -ne "`wc -c < 'lf.c'`"
  1009. then
  1010.     echo shar: error transmitting "'lf.c'" '(should have been 1252 characters)'
  1011. fi
  1012. fi
  1013. echo shar: extracting "'lf2cr.c'" '(219 characters)'
  1014. if test -f 'lf2cr.c'
  1015. then
  1016.     echo shar: will not over-write existing file "'lf2cr.c'"
  1017. else
  1018. cat << \SHAR_EOF > 'lf2cr.c'
  1019. main(argc,argv)
  1020. int argc;
  1021. char *argv[]; {
  1022.     char buf[512];
  1023.     register int i;
  1024.  
  1025.     for (i = 1; i < argc; i++) {
  1026.         strcpy(buf,"mv ");
  1027.         strcat(buf,LF2CR(argv[i]));
  1028.         strcat(buf," ");
  1029.         strcat(buf,argv[i]);
  1030.         system(buf);
  1031.     }
  1032. }
  1033. SHAR_EOF
  1034. if test 219 -ne "`wc -c < 'lf2cr.c'`"
  1035. then
  1036.     echo shar: error transmitting "'lf2cr.c'" '(should have been 219 characters)'
  1037. fi
  1038. fi
  1039. echo shar: extracting "'receive.c'" '(4527 characters)'
  1040. if test -f 'receive.c'
  1041. then
  1042.     echo shar: will not over-write existing file "'receive.c'"
  1043. else
  1044. cat << \SHAR_EOF > 'receive.c'
  1045. /*
  1046. **    receive filename
  1047. */
  1048. #
  1049. # include <file.h>
  1050. # include <curses.h>
  1051. # include <signal.h>
  1052. # include <time.h>
  1053. # include <setjmp.h>
  1054.  
  1055. # define    SOH    0x1
  1056. # define    NAK    0x15
  1057. # define    ACK    0x6
  1058. # define    EOT    0x4
  1059. # define    CANCEL    0x18
  1060.  
  1061. char filename[64];
  1062. int retries;        /* retries counter */
  1063. int seq_num;        /* sequence number */
  1064. int caught();        /* interrupt routine, called if haven't received a
  1065.                block in 1 second */
  1066. struct sigvec vec;
  1067. jmp_buf env;
  1068.  
  1069. main(argc,argv)
  1070. int argc;
  1071. char *argv[]; {
  1072.  
  1073.     int fd;
  1074.     char c;
  1075.     register int i;
  1076.  
  1077.     if (!((argc == 2) || (argc == 3))) {
  1078.         printf("ILLEGAL NUMBER OF ARGUMENTS.\n");
  1079.         printf("SYNOPSIS: to_vax_x [-t] [-m] filename\n");
  1080.         exit(-1);
  1081.     }
  1082.  
  1083.     if (argc == 3) {
  1084.         if (strcmp(argv[1],"-t") && strcmp(argv[1],"-m")) {
  1085.             printf("bad argument `%s'\n",argv[1]);
  1086.             printf("SYNOPSIS: to_vax_x [-t] [-m] filename\n");
  1087.             exit(-1);
  1088.         }
  1089.     }
  1090.  
  1091.     strcpy(filename,argv[argc-1]);
  1092.     if ((fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0) {
  1093.         perror(filename);
  1094.         exit(-1);
  1095.     }
  1096.  
  1097.     initscr();
  1098.     raw();            /* set terminal to 8-bit I/O */
  1099.     noecho();
  1100.  
  1101. /*
  1102. **    Ignore interrupts from the user.
  1103. **    If the user could delete this program the terminal
  1104. **    would be left in a undiserable state of mind.
  1105. */
  1106.  
  1107.     vec.sv_handler = caught;
  1108.     vec.sv_mask = vec.sv_onstack = 0;
  1109.     sigvec(SIGALRM,&vec,(struct sigvec *)0);
  1110.  
  1111.     sigsetmask(-1 ^ (1 << (SIGALRM-1)));    /* enable alarm signal */
  1112.  
  1113.     c = NAK;
  1114.     retries = 0;
  1115.     if (setjmp(env)) {
  1116.         write(1,&c,1);
  1117.         if (retries++ > 10) {
  1118.         reset(1);          /* restore terminal to normal state */
  1119.         printf("Can not get reply from transmitter\n");
  1120.         exit(0); 
  1121.         }
  1122.     }
  1123.     
  1124.     SET_TIMER(10);
  1125.     read(0,&c,1);
  1126.     DISABLE_TIMER();
  1127.  
  1128.     seq_num = 1; retries = 0;
  1129.     for(;;) {
  1130.  
  1131.         if (retries == 10) {
  1132.             c = CANCEL;
  1133.             write(1,&c,1);
  1134.             reset(1);
  1135.             printf("Terminated after 10 retries\r\n");
  1136.             exit(-1);
  1137.         }
  1138.  
  1139.         switch( c & 0x7f ) {
  1140.             case SOH:
  1141.                 BLOCK(fd);
  1142.                 break;
  1143.             case CANCEL:
  1144.                 printf("Received CANCEL signal\r\n");
  1145.                 reset(1);
  1146.                 exit(-1);
  1147.             case EOT:
  1148.                 c = ACK;
  1149.                 write(1,&c,1);
  1150.                 reset(0);
  1151.                 goto END;
  1152.             default:
  1153.                 sleep(2);    /* wait for a bit */
  1154.                 noraw();    /* flush input buffer */
  1155.                 raw();
  1156.                 c = NAK;
  1157.                 write(1,&c,1);
  1158.                 retries++;
  1159.                 break;
  1160.         }
  1161.         c = NAK;
  1162.         if (setjmp(env)) {
  1163.             write(1,&c,1);
  1164.             if (retries++ > 10) {
  1165.             reset(1);          /* restore terminal to normal state */
  1166.             printf("Can not get reply from transmitter\n");
  1167.             exit(0); 
  1168.             }
  1169.         }
  1170.     
  1171.         SET_TIMER(10);
  1172.         read(0,&c,1);
  1173.             DISABLE_TIMER();
  1174.         
  1175.     }
  1176. END:
  1177.  
  1178.     close(fd);
  1179.     if (argc == 3) {
  1180.         switch( argv[1][1] ) {
  1181.             case 't':
  1182.                 printf("Deleting carriage returns\n");
  1183.                 DELCR(filename);
  1184.                 break;
  1185.             case 'm':
  1186.                 printf("Changing cr's to lf's\n");
  1187.                 CR2LF(filename);
  1188.         }
  1189.     }
  1190. }
  1191.  
  1192. BLOCK(fd) 
  1193. int fd; {
  1194.     
  1195.     register i;
  1196.     register char *p;
  1197.     char block[131];
  1198.     char c;
  1199.     int xsum;
  1200.  
  1201.     retries = 0;        /* start fresh */
  1202.  
  1203.     /* used to be 1 */
  1204.     SET_TIMER(10);
  1205.     if (setjmp(env)) {
  1206.         noraw();
  1207.         raw();
  1208.         c = NAK;
  1209.         write(1,&c,1);
  1210.         retries++;
  1211.         return;
  1212.     } else {
  1213.         i = read(0,block,131);
  1214.         while( i < 131 ) {
  1215.             i += read(0,&block[i],131-i);    /* data block */
  1216.         }
  1217.         DISABLE_TIMER();
  1218.     }
  1219.     
  1220.     if ( (block[0] & 0xff) != seq_num ) {
  1221.         i = (seq_num ? seq_num - 1 : 255);
  1222.         if ((block[0] & 0xff) != i ) {
  1223.             if ( (block[1] & 0xff) != (255 - seq_num) ) {
  1224.                 i = 255 - i;
  1225.                 if ((block[1] & 0xff) == i ) {
  1226.                     c = NAK;
  1227.                     write(1,&c,1);
  1228.                     retries++;
  1229.                     return;
  1230.                 }
  1231.                 c = CANCEL;     /* loss of synchronization */
  1232.                 write(1,&c,1);
  1233.                 reset(1);
  1234.                 exit(-1);
  1235.             }
  1236.         }
  1237.         retries++;
  1238.         goto leave;
  1239.     }
  1240.  
  1241.     if ( (block[1] & 0xff) != (255 - seq_num) ) {
  1242.         c = NAK;
  1243.         write(1,&c,1);
  1244.         retries++;
  1245.         return;
  1246.     }
  1247.     
  1248.     xsum = 0;
  1249.     for (i = 0, p = &block[2]; i < 128; i++) xsum += (*p++ & 0xff);
  1250.     xsum &= 0xff;
  1251.     if ( (block[130] & 0xff) != xsum ) {
  1252.         c = NAK;
  1253.         write(1,&c,1);
  1254.         retries++;
  1255.         return;
  1256.     }
  1257.  
  1258.     write(fd,&block[2],128);
  1259.     retries = 0;
  1260.     seq_num = ++seq_num % 256;
  1261. leave:
  1262.     c = ACK;
  1263.     write(1,&c,1);
  1264. }
  1265.  
  1266. reset(remove) 
  1267. int remove; {
  1268.     
  1269.     DISABLE_TIMER();
  1270.     sigsetmask(0);
  1271.     vec.sv_handler = SIG_DFL;
  1272.     vec.sv_mask = vec.sv_onstack = 0;
  1273.     sigvec(SIGALRM,&vec,(struct sigvec *)0);
  1274.     noraw(); echo();
  1275.     endwin();
  1276.     if (remove) unlink(filename);
  1277. }
  1278.     
  1279.  
  1280. caught() {
  1281.     longjmp(env,1);
  1282. }
  1283.  
  1284. SET_TIMER(secs) 
  1285. int secs; {
  1286.     struct itimerval val;
  1287.  
  1288.     val.it_value.tv_sec = secs;
  1289.     val.it_value.tv_usec = secs * 1000;
  1290.     timerclear(&val.it_interval);
  1291.     setitimer(ITIMER_REAL,&val,(struct itimerval *)0);
  1292. }
  1293.  
  1294. DISABLE_TIMER() {
  1295.     struct itimerval val;
  1296.  
  1297.     timerclear(&val.it_value);
  1298.     setitimer(ITIMER_REAL,&val,(struct itimerval *)0);
  1299. }
  1300. SHAR_EOF
  1301. if test 4527 -ne "`wc -c < 'receive.c'`"
  1302. then
  1303.     echo shar: error transmitting "'receive.c'" '(should have been 4527 characters)'
  1304. fi
  1305. fi
  1306. echo shar: extracting "'to_pc.c'" '(5027 characters)'
  1307. if test -f 'to_pc.c'
  1308. then
  1309.     echo shar: will not over-write existing file "'to_pc.c'"
  1310. else
  1311. cat << \SHAR_EOF > 'to_pc.c'
  1312. #
  1313. # include <curses.h>
  1314. # include <signal.h>
  1315. # include <file.h>
  1316.  
  1317. # define    ST_BLK    33        /* ! */
  1318. # define    READY    34        /* " */
  1319. # define    OKAY    35        /* # */
  1320. # define    NOT_OK    36        /* $ */
  1321. # define    CANCEL    37        /* % */
  1322. # define    CR    13        /* \r */
  1323. # define    pad "    "
  1324.  
  1325. int seq_num, old, flags;
  1326. char buf[95], seq[4], filename[64];
  1327.  
  1328. main(argc,argv)
  1329. int argc;
  1330. char *argv[]; {
  1331.  
  1332.     int fd;
  1333.     register i;
  1334.     register char *ptr;
  1335.     char *ADDCR();
  1336.     char c, chunck[45];
  1337.     char *MK_SEQ(), *MK_START(), *itos();
  1338.     int lim, pid;
  1339.     int first = 1;
  1340.  
  1341.     flags = argc;
  1342.     if (!((argc == 2) || (argc == 3))) {
  1343.         printf("ILLEGAL NUMBER OF ARGUMENTS.\n");
  1344.         printf("SYNOPSIS: to_pc [-t] filename\n");
  1345.         exit(-1);
  1346.     }
  1347.  
  1348.     if (argc == 2) strcpy(filename,argv[1]);  /* don't put in CR's */
  1349.     else { 
  1350.         if (strcmp(argv[1],"-t")) {
  1351.             printf("bad argument `%s'\n",argv[1]);
  1352.             printf("SYNOPSIS: to_pc [-t] filename\n");
  1353.             exit(-1);
  1354.         }
  1355.         printf("Adding carriage returns, wait .......\n");
  1356.         strcpy(filename,ADDCR(argv[2]));
  1357.     }
  1358.  
  1359.     if ((fd = open(filename,O_RDONLY,0644)) < 0) {
  1360.         perror(filename);
  1361.         exit(-1);
  1362.     }
  1363.  
  1364.     printf("\nPress Alt-F6 to transmit file to PC.\n");
  1365.  
  1366.     initscr();
  1367.     crmode();    /* set terminal to unbuffered I/O */
  1368.  
  1369. /*
  1370. **    Ignore interrupts from the user.
  1371. **    If the user could delete this program the terminal
  1372. **    would be left in a undiserable state of mind.
  1373. */
  1374.     sigsetmask( -1 );    /* ignore all signals */
  1375.  
  1376.     if ((pid = fork()) == 0) {   /* child sends ready prompt */
  1377.         sleep(8); i = 0;
  1378.         strcpy(buf,"\"\r");
  1379.         while (1) {
  1380.             write(1, buf, 2);
  1381.             sleep(2);
  1382.             if ( i++ == 30 ) break;  /* not receiving ready character */
  1383.         }
  1384.         RESET();         /* restore terminal to normal state */
  1385.         kill(0,SIGKILL);    /* kill all processes */
  1386.     }
  1387.  
  1388.     for(;;) {          /* wait for ready character */
  1389.         READ(buf);    /* if not received in 68 sec's, program terminates */
  1390.         if (buf[0] == OKAY) break;
  1391.     }
  1392.  
  1393.     kill(pid,SIGKILL);  /* received ok response, so kill child */
  1394.     wait(0);
  1395.  
  1396.     seq_num = 0; strcpy(chunck,argv[argc-1]); lim = strlen(chunck);
  1397.     do {             /* make 90 character block */
  1398.  
  1399.         ptr = MK_START();            /* make start of block */
  1400.         if (first) {            /* header record */
  1401.             strcpy(ptr,"HH");
  1402.             strcat(ptr,chunck);
  1403.             ptr = lim + &buf[6];
  1404.             first = 0;
  1405.         } else {
  1406.             if (lim < 5) strcat(ptr,"0");
  1407.             strcat(ptr,itos( 2 * lim ));
  1408.             ptr = &buf[6];
  1409.             for (i = 0; i < lim; i++) {
  1410.                 c = (chunck[i] & 0xf0) / 16 + '0';
  1411.                 *ptr++ = c > '9' ? c + 7 : c;
  1412.                 c = (chunck[i] & 0xf) + '0';
  1413.                 *ptr++ = c > '9' ? c + 7 : c;
  1414.             }
  1415.         }
  1416.  
  1417.         strcat(ptr,MK_SEQ(MK_XSUM(ptr)));
  1418.  
  1419.         WRITE();
  1420.         WAIT(chunck);
  1421.         ERR_CHECK(chunck);
  1422.  
  1423.     } while ( (lim = read(fd,chunck,40)) > 0 );
  1424.  
  1425.     ptr = MK_START();
  1426.     strcat(ptr,"EE");
  1427.     ptr = &buf[6];
  1428.     strcat(ptr,MK_SEQ(MK_XSUM(ptr)));
  1429.     WRITE();
  1430.     READ(chunck);
  1431.     ERR_CHECK(chunck);
  1432.  
  1433.     RESET();
  1434.  
  1435.     close(fd);
  1436. }
  1437.  
  1438. WAIT(s)
  1439. char *s; {
  1440.     int pid;
  1441.     register i;
  1442.  
  1443.     if ((pid = fork()) == 0) {
  1444.         i = 1;
  1445.         while (i++ < 10) {
  1446.             WRITE();
  1447.         }
  1448.         sleep(10);
  1449.         LEAVE();
  1450.         kill(0,SIGKILL);    /* kill all processes */
  1451.     }
  1452.  
  1453.     READ(s);
  1454.     kill(pid,SIGKILL);          /* received response, kill child */
  1455.     wait(0);
  1456. }
  1457.  
  1458.  
  1459. MK_XSUM(s) 
  1460. char *s; {
  1461.     register xsum;
  1462.     register char *ptr;
  1463.  
  1464.     ptr = s;
  1465.     while ( ptr < &buf[86] ) *ptr++ = ' ';   /* pad buffer with blanks */
  1466.     xsum = 0; *ptr = '\0'; ptr = buf;
  1467.     while ( *ptr )               /* compute check sum */
  1468.         xsum = (xsum ^ *ptr++) & 0177;
  1469.     return(xsum);
  1470. }
  1471.  
  1472. char *MK_START() {
  1473.     register char *ptr;
  1474.  
  1475.     ptr = buf;
  1476.     *ptr = ST_BLK; *(ptr + 1) = '\0';
  1477.     seq_num = ++seq_num % 1000;
  1478.     strcpy(seq,MK_SEQ(seq_num));
  1479.     strcat(ptr,seq);
  1480.     return( &buf[4] );       /* return address to record type */
  1481. }
  1482.  
  1483. ERR_CHECK(resp)
  1484. char *resp; {
  1485.     char ok[6];
  1486.     int count = 1;
  1487.  
  1488.     while (*resp == NOT_OK) {
  1489.         if (count++ > 10) {     /* tried 10 times so exit */
  1490.             LEAVE(); exit(0);
  1491.         }
  1492.         WRITE();
  1493.         WAIT(resp);
  1494.     }
  1495.  
  1496.     strcpy(ok,"#"); strcat(ok,seq); strcat(ok,"#");
  1497.     count = 1;
  1498.     while (strncmp(ok,resp,5)) {
  1499.         /* used to be a literal control X */
  1500.         if (*resp == '\030') { /* user terminated transfer Alt-F6 */
  1501.             RESET(); exit(0);
  1502.         }
  1503.         if (*resp == CANCEL) { /* programs not in synch */
  1504.             RESET(); exit(0);
  1505.         }  
  1506.         if (count++ > 10) {    /* gave up, tried ten times */
  1507.             LEAVE(); exit(0);
  1508.         }
  1509.         WRITE();
  1510.         WAIT(resp);
  1511.     }
  1512. }
  1513.  
  1514.  
  1515. LEAVE() {
  1516.     register char *ptr;
  1517.  
  1518.     ptr = MK_START();
  1519.     strcat(ptr,"XX");
  1520.     ptr = &buf[6];
  1521.     strcat(ptr,MK_SEQ(MK_XSUM(ptr)));
  1522.     WRITE();
  1523.     RESET();
  1524. }
  1525.  
  1526.  
  1527. char *MK_SEQ(i)
  1528. int i; {
  1529.     char *itos();
  1530.     static char s[3];
  1531.  
  1532.     if ( i < 100 ) strcpy(s,"0");
  1533.     else s[0] = '\0';
  1534.     if ( i < 10 ) strcat(s,"0");
  1535.     strcat(s,itos(i));
  1536.     return( s );
  1537. }
  1538.  
  1539. READ(s)
  1540. register char *s; {
  1541.     
  1542.     char c;
  1543.     while (read( 0, s++, 1) > 0) {      /* read characters until a CR is read */
  1544.         c = *(s-1);
  1545.         if ((c == '\n') || (c == CR)) break;
  1546.     }
  1547.     *s = '\0';
  1548. }
  1549.  
  1550. WRITE() {
  1551.  
  1552.     buf[89] = CR; buf[90] = '\0';
  1553.     write( 1, buf, 90);
  1554. }
  1555.  
  1556. RESET() {
  1557.  
  1558.     if (flags == 3) unlink(filename);
  1559.     endwin();
  1560. }
  1561.  
  1562. char *itos(i1)
  1563. int    i1;
  1564. {
  1565.     register char    *a;
  1566.     register int    i;
  1567.     static char    buf[10];
  1568.  
  1569.     i = i1;
  1570.     if (i < 0)
  1571.         i = -i;
  1572.  
  1573.     a = &buf[sizeof buf - 1];
  1574.     *a-- = '\0';
  1575.     do
  1576.     {
  1577.         *a-- = i % 10 + '0';
  1578.         i /= 10;
  1579.     } while (i);
  1580.     if (i1 < 0)
  1581.         *a-- = '-';
  1582.  
  1583.     a++;
  1584.     return (a);
  1585. }
  1586. SHAR_EOF
  1587. if test 5027 -ne "`wc -c < 'to_pc.c'`"
  1588. then
  1589.     echo shar: error transmitting "'to_pc.c'" '(should have been 5027 characters)'
  1590. fi
  1591. fi
  1592. echo shar: extracting "'to_vax.c'" '(3655 characters)'
  1593. if test -f 'to_vax.c'
  1594. then
  1595.     echo shar: will not over-write existing file "'to_vax.c'"
  1596. else
  1597. cat << \SHAR_EOF > 'to_vax.c'
  1598. #
  1599. # include <file.h>
  1600. # include <curses.h>
  1601. # include <signal.h>
  1602.  
  1603. # define    ST_BLK    33
  1604. # define    READY    34
  1605. # define    OKAY    35
  1606. # define    NOT_OK    36
  1607. # define    CANCEL    37
  1608. # define    CR    13
  1609. # define    ctoi(c)    (c - '0' > 9 ? c - '0' - 7 : c - '0')
  1610.  
  1611. char filename[64];
  1612. int old;
  1613. main(argc,argv)
  1614. int argc;
  1615. char *argv[]; {
  1616.  
  1617.     int fd;
  1618.     register i;
  1619.     register char *ptr, *op;
  1620.     char buf[512], seq[4];
  1621.     char *index();
  1622.     int xsum, lim, old_seq, pid;
  1623.  
  1624.     if (!((argc == 2) || (argc == 3))) {
  1625.         printf("ILLEGAL NUMBER OF ARGUMENTS.\n");
  1626.         printf("SYNOPSIS: to_vax [-t] filename\n");
  1627.         exit(-1);
  1628.     }
  1629.  
  1630.     if (argc == 3) {
  1631.         if (strcmp(argv[1],"-t")) {
  1632.             printf("bad argument `%s'\n",argv[1]);
  1633.             printf("SYNOPSIS: to_vax [-t] filename\n");
  1634.             exit(-1);
  1635.         }
  1636.     }
  1637.  
  1638.     strcpy(filename,argv[argc-1]);
  1639.     if ((fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0) {
  1640.         perror(filename);
  1641.         exit(-1);
  1642.     }
  1643.  
  1644.     initscr();
  1645.     crmode();        /* set terminal to unbuffered I/O */
  1646.  
  1647.     printf("\nPress Alt-F7 to transmit file to VAX.\n");
  1648.  
  1649.  
  1650. /*
  1651. **    Ignore interrupts from the user.
  1652. **    If the user could delete this program the terminal
  1653. **    would be left in a undiserable state of mind.
  1654. */
  1655.     sigsetmask( -1 );    /* ignore all signals */
  1656.  
  1657.     if ((pid = fork()) == 0) {
  1658.         sleep(100);
  1659.         RESET(fd);
  1660.         kill(0, SIGKILL);     /* Can't wait forever for a READY reply */
  1661.     }
  1662.  
  1663.     ptr = buf;
  1664.     while (1) {              /* wait for ready character */
  1665.         read(0,ptr,1);
  1666.         if (*ptr == READY) break;
  1667.     }
  1668.     kill(pid, SIGKILL);
  1669.     wait(0);
  1670.     read(0,ptr,1);            /* get the CR from the READY */
  1671.     WCHAR(OKAY); WCHAR(CR);
  1672.  
  1673.     for(;;) {
  1674. input:
  1675.         if ((pid = fork()) == 0) {
  1676.             i = 0;
  1677.             while ( i++ < 10 ) {
  1678.                 sleep(10);
  1679.                 WCHAR(NOT_OK); WCHAR(CR);
  1680.             }
  1681.             sleep(10);
  1682.             RESET(fd);
  1683.             kill(0, SIGKILL);      /* kill all processes */
  1684.         }
  1685.         READ(buf);            /* read data block */
  1686.         kill( pid, SIGKILL );          /* kill child */
  1687.         wait(0);
  1688.  
  1689.         /* check for 90 character block */
  1690.         ptr = index(buf,ST_BLK);
  1691.         if (strlen(ptr) != 90) {
  1692.             WCHAR(NOT_OK); WCHAR(CR);
  1693.             goto input;
  1694.         }
  1695.  
  1696.         xsum = 0;
  1697.         while ( ptr < &buf[86] )  /* compute check sum */
  1698.             xsum = (xsum ^ *ptr++) & 0177;
  1699.         if ( xsum != atoi(ptr) ) {     /* check sum not ok */
  1700.             WCHAR(NOT_OK); WCHAR(CR);
  1701.             goto input;
  1702.         }
  1703.  
  1704.         ptr = index(buf,ST_BLK) + 1;
  1705.         strncpy(seq,ptr,3);
  1706.         switch( (lim = atoi(seq)) - old_seq ) {
  1707.             case 0:            /* reply garbled, resend reply */
  1708.                 WCHAR(OKAY); WSTR(seq); WCHAR(OKAY); WCHAR(CR);
  1709.                 goto input;
  1710.             case 1: case -999:      /* sequence number ok */
  1711.                 old_seq = lim;
  1712.                 break;
  1713.             default:             /* fatal loss of synchronization */
  1714.                 WCHAR(CANCEL); WCHAR(CANCEL); WCHAR(CANCEL); WCHAR(CR);
  1715.                 RESET(fd);
  1716.                 exit(-1);
  1717.         }
  1718.         
  1719.         ptr += 3;
  1720.         if (!(strncmp(ptr,"XX",2) && strncmp(ptr,"EE",2))) break;
  1721.  
  1722.         if (strncmp(ptr,"HH",2)) {      /* decode data block */
  1723.             lim = ctoi(*ptr) * 10 + ctoi(*(ptr+1));
  1724.             op = buf;
  1725.             for ( i = 2; i < lim + 2; i += 2 )
  1726.                 *op++ = ctoi( *(ptr+i) ) * 16 + ctoi( *(ptr+i+1) );
  1727.             write(fd, buf, lim/2);     /* write data block to file */
  1728.         }
  1729.  
  1730.         WCHAR(OKAY); WSTR(seq); WCHAR(OKAY); WCHAR(CR);
  1731.     } /* end while */
  1732.  
  1733.     WCHAR(OKAY); WSTR(seq); WCHAR(OKAY); WCHAR(CR);
  1734.     nocrmode();
  1735.     endwin();
  1736.     close(fd);
  1737.     if (argc == 3) {
  1738.         printf("Deleting carriage returns\n");
  1739.         DELCR(filename);
  1740.     }
  1741. } /* end main */
  1742.  
  1743. WCHAR(c)
  1744. char c; {   /* write a character to standard output */
  1745.  
  1746.     write(1, &c, 1);
  1747. }
  1748.  
  1749. WSTR(s)
  1750. char *s; {   /* write a string to standard output */
  1751.     
  1752.     write(1, s, strlen(s));
  1753. }
  1754.  
  1755. READ(s) 
  1756. char *s; {
  1757.     register char *ptr;
  1758.  
  1759.     ptr = s;
  1760.     while ( read(0, ptr, 1) > 0 ) {       /* read characters until CR */
  1761.         if (*ptr++ == CR) break;
  1762.     }
  1763.     *ptr = '\0';
  1764. }
  1765.  
  1766. RESET(fd) 
  1767. int fd; {            /* put terminal back into start up state */
  1768.  
  1769.     nocrmode();
  1770.     endwin();
  1771.     close(fd);
  1772.     unlink(filename);
  1773. }
  1774. SHAR_EOF
  1775. if test 3655 -ne "`wc -c < 'to_vax.c'`"
  1776. then
  1777.     echo shar: error transmitting "'to_vax.c'" '(should have been 3655 characters)'
  1778. fi
  1779. fi
  1780. echo shar: extracting "'transmit.c'" '(2908 characters)'
  1781. if test -f 'transmit.c'
  1782. then
  1783.     echo shar: will not over-write existing file "'transmit.c'"
  1784. else
  1785. cat << \SHAR_EOF > 'transmit.c'
  1786. /*
  1787. **    transmit filename
  1788. */
  1789. # include <curses.h>
  1790. # include <file.h>
  1791. # include <signal.h>
  1792. # include <time.h>
  1793.  
  1794. # define    SOH    0x1
  1795. # define    NAK    0x15
  1796. # define    ACK    0x6
  1797. # define    EOT    0x4
  1798. # define    CANCEL    0x18
  1799.  
  1800. int flags;
  1801. char filename[64];
  1802.  
  1803. main(argc,argv)
  1804. int argc;
  1805. char *argv[]; {
  1806.  
  1807.     int fd;
  1808.     register i;
  1809.     register char *p;
  1810.     char block[132];
  1811.     char c;
  1812.     int length, pid;
  1813.     int seq_num, xsum;
  1814.     int retries;
  1815.     struct sigvec vec;
  1816.     int caught();
  1817.  
  1818.     flags = argc;
  1819.     if (!((argc == 2) || (argc == 3))) {
  1820.         printf("ILLEGAL NUMBER OF ARGUMENTS.\n");
  1821.         printf("SYNOPSIS: to_pc_x [-t] [-m] filename\n");
  1822.         exit(-1);
  1823.     }
  1824.  
  1825.     if (argc == 2) strcpy(filename,argv[1]);  /* don't put in CR's */
  1826.     else { 
  1827.         switch (argv[1][1]) {
  1828.             case 't':
  1829.                 printf("Adding carriage returns, wait .......\n");
  1830.                 strcpy(filename,ADDCR(argv[2]));
  1831.                 printf("DONE\n");
  1832.                 break;
  1833.             case 'm':
  1834.                 printf("Changing lf's to cr's, wait .......\n");
  1835.                 strcpy(filename,LF2CR(argv[2]));
  1836.                 printf("DONE\n");
  1837.                 break;
  1838.             default:
  1839.                 printf("bad argument `%s'\n",argv[1]);
  1840.                 printf("SYNOPSIS: to_pc_x [-t] [-m] filename\n");
  1841.                 exit(-1);
  1842.         }
  1843.     }
  1844.  
  1845.     if ((fd = open(filename,O_RDONLY,0644)) < 0) {
  1846.         perror(filename);
  1847.         exit(-1);
  1848.     }
  1849.  
  1850.     initscr();
  1851.     raw();            /* set terminal to 8-bit I/O */
  1852.     noecho();
  1853.  
  1854. /*
  1855. **    Ignore interrupts from the user.
  1856. **    If the user could delete this program the terminal
  1857. **    would be left in a undiserable state of mind.
  1858. */
  1859.     vec.sv_handler = caught;
  1860.     vec.sv_mask = vec.sv_onstack = 0;
  1861.     sigvec(SIGALRM,&vec,(struct sigvec *)0);
  1862.  
  1863.     sigsetmask(-1 ^ (1 << (SIGALRM-1)));
  1864.  
  1865.     SET_TIMER();
  1866.  
  1867.     for(;;) {          /* wait for NAK */
  1868.         read(0,&c,1);    /* if not received in 60 sec's, program terminates */
  1869.         if ((c&0x7f) == NAK) break;
  1870.     }
  1871.  
  1872.     DISABLE_TIMER();
  1873.  
  1874.     seq_num = 1;
  1875.     p = &block[3];
  1876.     while ( (length = read(fd,p,128)) > 0 ) {
  1877.  
  1878.         p = block;
  1879.         *p++ = SOH;
  1880.         *p++ = seq_num;
  1881.         *p++ = 255 - seq_num;
  1882.  
  1883.         p += length;
  1884.         for( i = length; i < 128; i++) *p++ = 0;
  1885.         xsum = 0;
  1886.         for (i = 3; i < 131; i++) xsum += block[i];
  1887.         *p = xsum & 0xff;
  1888.  
  1889.         retries = 0;
  1890.         do {
  1891.             if (retries) sleep(10);
  1892.  
  1893.             write(1,block,132);
  1894.             SET_TIMER();
  1895.             read(0,&c,1);
  1896.             DISABLE_TIMER();
  1897.  
  1898.             if ( (c&0x7f) == CANCEL ) {RESET(); exit(-1);}
  1899.             if (retries++ > 10 ) {
  1900.                 c = CANCEL;
  1901.                 write(1,&c,1);
  1902.                 RESET(); exit(-1);
  1903.             }
  1904.         } while ( (c&0x7f) != ACK );
  1905.  
  1906.         seq_num = ++seq_num % 256;
  1907.         p = &block[3];
  1908.             
  1909.     }
  1910.  
  1911.     SET_TIMER();
  1912.     do {
  1913.         c = EOT;
  1914.         write(1,&c,1);
  1915.         read(0,&c,1);
  1916.     } while ( (c&0x7f) != ACK );
  1917.     DISABLE_TIMER();
  1918.  
  1919.     RESET();
  1920. }
  1921.  
  1922. RESET() {
  1923.  
  1924.     noraw();
  1925.     echo();
  1926.     endwin();
  1927.     if (flags == 3) unlink(filename);
  1928. } /* END */
  1929.  
  1930. caught() {
  1931.     RESET();
  1932.     kill(0,SIGKILL);
  1933. }
  1934.  
  1935. SET_TIMER() {
  1936.     struct itimerval val;
  1937.  
  1938.     val.it_value.tv_sec = 60;
  1939.     val.it_value.tv_usec = 60 * 1000;
  1940.     timerclear(&val.it_interval);
  1941.     setitimer(ITIMER_REAL,&val,(struct itimerval *)0);
  1942. }
  1943.  
  1944. DISABLE_TIMER() {
  1945.     struct itimerval val;
  1946.  
  1947.     timerclear(&val.it_value);
  1948.     setitimer(ITIMER_REAL,&val,(struct itimerval *)0);
  1949. }
  1950. SHAR_EOF
  1951. if test 2908 -ne "`wc -c < 'transmit.c'`"
  1952. then
  1953.     echo shar: error transmitting "'transmit.c'" '(should have been 2908 characters)'
  1954. fi
  1955. fi
  1956. exit 0
  1957. #    End of shell archive
  1958.